package ihistoryApp.view;

import ihistoryApp.dal.entity.Question;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 76004
 * @apiNote
 * @date 2024/5/25 17:18
 */
@Data
@Builder
@NoArgsConstructor
@Slf4j
@AllArgsConstructor
public class BattleRoom {
    private String uidA;
    private String uidB;
    private List<Question> questionList;
    private Long scoreA = 0L;
    private Long scoreB = 0L;
    private int nowIndex = -1;
    private long expA = 0;
    private long expB = 0;
    private long completeScoreA = 0;
    private long completeScoreB = 0;
    /**
     * 记录每次答题开始的时间
     */
    private Long timestamp;
    /**
     * status=2的时候表示收到双方用户的下一题请求
     */
    private AtomicInteger status = new AtomicInteger(0);

    public AtomicInteger getStatus() {
        log.info("status:{}", status.get());
        return status;
    }

    public Question nextQuestion() {
        nowIndex++;
        if (nowIndex >= questionList.size()) {
            return null;
        }
        Question question = questionList.get(nowIndex);
        timestamp = System.currentTimeMillis();
        return question;
    }

    public Question getNowQuestion() {
        return questionList.get(nowIndex);
    }

    public Long calScore() {
        Long allScore = (nowIndex + 2) * 30L;
        Long answerDuration = System.currentTimeMillis() - timestamp;
        log.info("nowIndex:{}, lastTime:{}, nowTime:{},allScore:{}", nowIndex, timestamp, System.currentTimeMillis(),allScore);
        if (answerDuration >= 30000) {
            return 0L;
        }
        return allScore / (answerDuration / 1000 + 1);
    }

    public void addScore(String uid, long score) {
        if (uidA.equals(uid)) {
            scoreA += score;
        } else if(uid.equals(uidB)) {
            scoreB += score;
        }
    }

    public boolean isLastQuestion() {
        return nowIndex >= questionList.size() - 1;
    }
}
